#!/bin/bash

# 学生选课系统启动脚本

echo "学生选课系统启动脚本"
echo "========================"

# 检查MySQL服务
echo "检查MySQL服务..."
if ! pgrep mysqld > /dev/null; then
    echo "启动MySQL服务..."
    sudo systemctl start mysql
    sleep 2
fi

# 检查数据库是否存在
echo "检查数据库..."
mysql -u root -e "USE student_course_system;" 2>/dev/null
if [ $? -ne 0 ]; then
    echo "初始化数据库..."
    mysql -u root < database/init.sql
    if [ $? -eq 0 ]; then
        echo "数据库初始化成功"
    else
        echo "数据库初始化失败，请手动检查MySQL配置"
        exit 1
    fi
else
    echo "数据库已存在"
fi

# 编译项目
echo "编译项目..."
if [ ! -d "build" ]; then
    mkdir build
fi

cd build
cmake ..
if [ $? -ne 0 ]; then
    echo "CMake配置失败"
    exit 1
fi

make
if [ $? -ne 0 ]; then
    echo "编译失败"
    exit 1
fi

cd ..

echo "启动服务器..."
echo "服务器将在 http://localhost:8080 启动"
echo "按 Ctrl+C 停止服务器"

# 启动服务器
./build/StudentCourseSystem
